package com.asa.ui.G;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class MazeData {
	//迷宫是由字符数组组成的我们可以使用char数组
    private  char[][] maze;
    private int entranceX,entranceY;
    private int exitX,exitY;
    public boolean[][] path;
    public boolean[][] visited;
    public int[][] distance={{0,-1},{1,0},{0,1},{-1,0}};
	public int getEntranceX() {
		return entranceX;
	}
	public void setEntranceX(int entranceX) {
		this.entranceX = entranceX;
	}
	public int getEntranceY() {
		return entranceY;
	}
	public void setEntranceY(int entranceY) {
		this.entranceY = entranceY;
	}
	public int getExitX() {
		return exitX;
	}
	public void setExitX(int exitX) {
		this.exitX = exitX;
	}
	public int getExitY() {
		return exitY;
	}
	public void setExitY(int exitY) {
		this.exitY = exitY;
	}
	public MazeData(String fileName) {
		readFile(fileName);
	}
   public MazeData(String[][] basedata) {
		// TODO Auto-generated constructor stub
	   int r=basedata.length;
		int c=basedata[0].length;
		maze=new char[r][c];//整体的渲染
		path=new boolean[r][c];//路径
		visited=new boolean[r][c];//已经被访问过的
		for(int i=0;i<r;i++){//读取迷宫数据
			String[] strings = basedata[i];
			for(int j=0;j<basedata[i].length;j++) {
			    maze[i][j]=basedata[i][j].toCharArray()[0];//读取的每行转为字符数组

			}
		}
	   
	}
	//获取数组的行
	public int getMazeL(){
		return maze.length;
	}
  //获取数组的列
	public int getMazeW(){
		return maze[getMazeL()-1].length;
	}
	//获取数组的元素
	public char getMazeElement(int x,int y){
		if(isIllegal(x,y)){
			throw new IllegalArgumentException("数组越界");
		}
		return maze[x][y];
	}
	//判断数组是否越界
	public boolean isIllegal(int x,int y){
		return !(x>=0&&x<getMazeL()&&y>=0&&y<getMazeW());
	}
	//读取文件
	public void readFile(String fileName){
		BufferedReader buf=null;
		try{
		File file=new File(fileName);
		if(!file.exists()){
			throw new IllegalArgumentException("文件不存在");
		}
		buf=new BufferedReader(new InputStreamReader(new BufferedInputStream(new FileInputStream(file))));
		String[] num=buf.readLine().split(" ");//获取第一行
		int r=Integer.parseInt(num[0]);
		int c=Integer.parseInt(num[1]);
		maze=new char[r][c];//整体的渲染
		path=new boolean[r][c];//路径
		visited=new boolean[r][c];//已经被访问过的
		for(int i=0;i<r;i++){//读取迷宫数据
		    maze[i]=buf.readLine().toCharArray();//读取的每行转为字符数组
		}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if( buf!=null){
				try {
					 buf.close();
				} catch (IOException e) {
					
				}
			}
		}
	}
	//输出数组
	public void print(){
		System.out.println(getMazeL()+" "+getMazeW());
		for(int i=0;i<getMazeL();i++){
			for(int j=0;j<getMazeW();j++){
				System.out.print(getMazeElement(i, j));
			}
			System.out.println();
		}
	}
//	public static void main(String[] args) {
//		MazeData maze=new MazeData("maze_101_101.txt");
//		maze.print();
//	}
} 
